<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline" label-position="left">

      <el-form-item label="用户账号">
        <el-input v-model="userQuery.userName" style="width: 150px" placeholder="用户账号"/>
      </el-form-item>

      <el-form-item label="用户类型">
        <el-select
          v-model="userQuery.userType"
          style="width: 150px"
          placeholder="用户类型">
          <el-option
            v-for="option in userTypeOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="联系电话">
        <el-input v-model="userQuery.tel" style="width: 150px" placeholder="联系电话"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column label="用户账号" align="center" prop="userName" />

      <el-table-column label="用户名称" align="center" prop="userRealName"/>

      <el-table-column label="用户类型" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.userType == '1'">
            <el-tag type="warning" effect="plain"> 菜农 </el-tag>
          </div>
          <div v-else-if="scope.row.userType == '2'">
            <el-tag type="" effect="plain"> 菜企 </el-tag>
          </div>
          <div v-else-if="scope.row.userType == '3'">
            <el-tag type="success" effect="plain"> 菜商 </el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="联系方式" align="center" prop="tel" />

      <el-table-column label="地址" align="center" prop="address" />

      <el-table-column label="身份证正面" align="center" width="150px">
        <template slot-scope="scope">
          <div v-if="scope.row.idCardFront != undefined && scope.row.idCardFront != ''">
            <el-image :src="scope.row.idCardFront" :preview-src-list="[scope.row.idCardFront]" style="width: 120px; height: 80px"/>
          </div>
          <div v-else>
            无
          </div>
        </template>
      </el-table-column>

      <el-table-column label="身份证背面" align="center" width="150px">
        <template slot-scope="scope">
          <div v-if="scope.row.idCardBack != undefined && scope.row.idCardBack != ''">
            <el-image :src="scope.row.idCardBack" :preview-src-list="[scope.row.idCardBack]" style="width: 120px; height: 80px"/>
          </div>
          <div v-else>
            无
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button type="success" @click="judge(scope.row.id, '1')">通过</el-button>
          <el-button type="danger" @click="judge(scope.row.id, '-1')">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>

import user from '@/api/trace/user'
export default {
  data() {
    return {
      listLoading: false,
      userTypeOptions: [
        {
          value: 1,
          label: '菜农'
        }, {
          value: 2,
          label: '菜企'
        }, {
          value: 3,
          label: '菜商'
        }
      ],
      list: [],
      current: 1,
      limit: 5,
      total: 10,
      userQuery: {
        judgeFlag: '0'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(current = 1) {
      this.current = current
      user.getPageList(this.current, this.limit, this.userQuery)
        .then(response => {
          this.list = response.data.list
          this.total = response.data.total
        }).catch(response => {
          console.log('error')
        })
    },
    resetData() {
      this.userQuery = {
        judgeFlag: '0'
      }
      this.getList()
    },
    judge(userId, judgeFlag) {
      const data = {
        id: userId,
        judgeFlag: judgeFlag
      }
      user.judge(data).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.getList()
      }).catch(res => {
        this.$message({
          type: 'error',
          message: '操作失败!'
        })
      })
    }
  }
}
</script>
